package edu.wuster.blog.service.impl;

import edu.wuster.blog.entity.Admin;
import edu.wuster.blog.exception.MyException;
import edu.wuster.blog.mapper.AdminMapper;
import edu.wuster.blog.service.AdminService;
import edu.wuster.blog.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public String adminLogin(Admin admin) {
        Admin admin1 = adminMapper.selectLogin(admin);
        //非空判断
        if (admin1 == null)
            throw new MyException("管理员用户名或密码错误");

        //生成jwt并返回
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", admin1.getId());
        claims.put("username", admin1.getAdminname());
        return JWTUtils.getJWT(claims);
    }
}
